home *** CD-ROM | disk | FTP | other *** search
- //-----------------------------------------------
- // PrintWithMargins.cs ⌐ 2001 by Charles Petzold
- //-----------------------------------------------
- using System;
- using System.Drawing;
- using System.Drawing.Printing;
- using System.Windows.Forms;
-
- class PrintWithMargins: Form
- {
- public static void Main()
- {
- Application.Run(new PrintWithMargins());
- }
- public PrintWithMargins()
- {
- Text = "Imprimir con mßrgenes";
-
- Menu = new MainMenu();
- Menu.MenuItems.Add("&Archivo");
- Menu.MenuItems[0].MenuItems.Add("&Imprimir...",
- new EventHandler(MenuFilePrintOnClick));
- }
- void MenuFilePrintOnClick(object obj, EventArgs ea)
- {
- // Crear PrintDocument.
-
- PrintDocument prndoc = new PrintDocument();
-
- // Crear el cuadro de dißlogo y establecer la propiedad PrinterName.
-
- PrinterSelectionDialog dlg = new PrinterSelectionDialog();
- dlg.PrinterName = prndoc.PrinterSettings.PrinterName;
-
- // Mostrar el cuadro de dißlogo y volver si el resultado no es OK.
-
- if (dlg.ShowDialog() != DialogResult.OK)
- return;
-
- // Establcer PrintDocument con la impresora seleccionada.
-
- prndoc.PrinterSettings.PrinterName = dlg.PrinterName;
-
- // Establecer el resto de propiedades de PrintDocument y comenzar.
-
- prndoc.DocumentName = Text;
- prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
- prndoc.Print();
- }
- void OnPrintPage(object obj, PrintPageEventArgs ppea)
- {
- Graphics grfx = ppea.Graphics;
- RectangleF rectf = new RectangleF(
- ppea.MarginBounds.Left -
- (ppea.PageBounds.Width - grfx.VisibleClipBounds.Width) / 2,
- ppea.MarginBounds.Top -
- (ppea.PageBounds.Height - grfx.VisibleClipBounds.Height) / 2,
- ppea.MarginBounds.Width,
- ppea.MarginBounds.Height);
-
- grfx.DrawRectangle(Pens.Black, rectf.X, rectf.Y,
- rectf.Width, rectf.Height);
-
- grfx.DrawLine(Pens.Black, rectf.Left, rectf.Top,
- rectf.Right, rectf.Bottom);
-
- grfx.DrawLine(Pens.Black, rectf.Right, rectf.Top,
- rectf.Left, rectf.Bottom);
- }
- }
-